package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AppointDto;
import com.zzyl.dto.GetAppointPageDto;
import com.zzyl.entity.AppointCount;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.CostomerMapper;
import com.zzyl.service.CostomerAppointService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import static com.zzyl.enums.BasicEnum.APPOINT_ERROR;

@Service
public class CostomerAppointServiceImpl implements CostomerAppointService {
    @Autowired
    private CostomerMapper costomerMapper;

    @Override
    public ResponseResult appointControl(AppointDto dto) {
        Reservation reservation = costomerMapper.selectByMobile(dto);
        //判断是否预约过，约过看时间，没约过就加进去
        if (reservation != null) {
            //时间相同
            if (Objects.equals(dto.getTime(), reservation.getTime())) {
                throw  new BaseException(APPOINT_ERROR);
            } else {
                //添加数据库
                insertAppoint(dto);
                return ResponseResult.success("预约成功");
            }
        }
        //未预约过
        insertAppoint(dto);
        return ResponseResult.success("预约成功");
    }

    @Override
    public PageResponse getPageAppoint(GetAppointPageDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Long userId = UserThreadLocal.getUserId();
        dto.setCreateBy(userId);
        List<ReservationVo> reservationVoList = costomerMapper.getPageAppoint(dto);
        Page<ReservationVo> reservationVoList1 = (Page<ReservationVo>) reservationVoList;
        PageResponse<ReservationVo> pageResponse = new PageResponse<>();
        pageResponse.setTotal(reservationVoList1.getTotal());
        pageResponse.setRecords(reservationVoList1.getResult());
        return pageResponse;
    }

    @Override
    public void cancelAppoint(Long id) {
        Reservation reservation = new Reservation();
        reservation.setStatus(2);
        reservation.setId(id);
        costomerMapper.updateStatus(reservation);
    }

    @Override
    public List<AppointCount> getCountByTime(Long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        LocalDateTime max = localDateTime.toLocalDate().atTime(LocalTime.MAX);
        LocalDateTime min = localDateTime.toLocalDate().atTime(LocalTime.MIN);
      List<AppointCount> appointCountList= costomerMapper.getCountByTime(min,max);
      appointCountList.forEach(appointCount -> appointCount.setCount(6-appointCount.getCount()));
      return appointCountList;
    }

    @Override
    public Integer getCancelCount() {
        LocalDateTime max = LocalDate.now().atTime(LocalTime.MAX);
        LocalDateTime min = LocalDate.now().atTime(LocalTime.MIN);
        Long id = UserThreadLocal.getUserId();
        return   costomerMapper.getCancelCount(id,min,max);
    }

    //插入
    private void insertAppoint(AppointDto dto) {
        Reservation reservation = new Reservation();
        BeanUtils.copyProperties(dto, reservation);
        reservation.setStatus(0);
        costomerMapper.insertAppoint(reservation);
    }


}
